//主函数，导出
function density(f_value) {
	/* 入参 */
	var type = f_value.type; //类型
	var tem = f_value.tem; //温度
	var des = f_value.des; //密度
	
	var PS = Number(des);
	var t = Number(tem);
	var pc = Number(des);
	var tc = Number(tem);
	var x = pc + tc;
	if (x < 653 || x > 1075) {
		// this.popmsg = '数据超出范围'
		// this.openfail()
		return 'error'
		pc = 1075 - tc;
	}
	//中间变量值
	var Px = '';
	var P15 = '';
	var P20 = '';
	var VCF = '';
	var A15 = '';
	var Pzj = '';
	var VCF20 = '';
	Px = PS * Mdxz(t);
	P15 = Px + t;
	A15 = Pzxs(P15, type);
	VCF = Rlxzxs(A15, t);
	P15 = Px / VCF;
	do {
		Pzj = P15;
		A15 = Pzxs(P15, type);
		VCF = Rlxzxs(A15, t);
		P15 = Px / VCF;
	} while (Math.abs(P15 - Pzj) > 0.05);
	A15 = Pzxs(P15, type);
	VCF20 = rlxzxs20(A15, t);
	VCF = Rlxzxs(A15, t);
	P20 = (P15 * VCF) / VCF20;
	var p15xy = Number(P15).toFixed(1);
	var p20xy = Number(P20).toFixed(1);

/* 出参组合 */
	var result = [{
		name: '15℃密度',
		value: p15xy,
		type:'number'
	},
	{
		name:'20℃密度',
		value:p20xy,
		type:'number'
	}
	];


	return result
	

}
//辅助计算函数-不导出，内部调用
//密度辅助函数
//密度修正系数
function Mdxz(tem) {
	var t = Number(tem);
	var x = 1 - (2.3 * (t - 20)) / 100000 - (2 * (t - 20) * (t - 20)) / 100000000;
	return Number(x).toFixed(9);
}
//密度膨胀系数
function Pzxs(p15, type) {
	var A_K0 = 613.9723;
	var A_A = A_K0;
	var A_K1 = 0;
	var A_B = A_K1; //原油610.5-1075.0
	var B1_KO = 186.9696;
	var B1_A = B1_KO;
	var B1_K1 = 0.4862;
	var B1_B = B1_K1; //石油产品839.0-1075.0
	var B2_KO = 594.5418;
	var B2_A = B2_KO;
	var B2_K1 = 0;
	var B2_B = B2_K1; //石油产品788.0-838.5
	var B3_KO = 346.4228;
	var B3_A = B3_KO;
	var B3_K1 = 0.4388;
	var B3_B = B3_K1; //石油产品653.0-770.0
	var C_KO = 0;
	var C_A = C_KO;
	var C_K1 = 0.6278;
	var C_B = C_K1; //润滑油800.0-1164.0
	var D_KO = -0.00336312;
	var D_A = D_KO;
	var D_K1 = 2680.321;
	var D_B = D_K1; //特殊油品770.5-787.5
	var R = 0.8; //通用系数
	if (type == '原油') {
		//原油计算公式
		var x = A_K0 / (p15 * p15);

		return Number(x).toFixed(9);
	} else if (type == '润滑油') {
		//润滑油计算公式
		var x = C_B / p15;
		return Number(x).toFixed(9);
	} //石油产品计算公式
	else {
		if (p15 > 653.0 && p15 < 770.0) {
			var x = B3_KO / (p15 * p15) + B3_K1 / p15;
			return Number(x).toFixed(9);
		} else if (p15 > 770 && p15 < 787.5) {
			var x = D_B / (p15 * p15) + D_A;
			return Number(x).toFixed(9);
		} else if (p15 > 787.5 && p15 < 838.5) {
			var x = B2_KO / (p15 * p15) + B2_K1 / p15;
			return Number(x).toFixed(9);
		} else if (p15 > 838.5 && p15 < 1075) {
			var x = B1_KO / (p15 * p15) + B1_K1 / p15;
			return Number(x).toFixed(9);
		} else {
			var x = B1_KO / (p15 * p15) + B1_K1 / p15;
			return Number(x).toFixed(9);
		}
	}
}
//密度容量修正系数VCF20
function rlxzxs20(a15, t) {
	var zr = 2.718282;
	var a = -a15 * (t - 20) * (1 + 0.8 * a15 * (t - 20) + 8 * a15);
	a = Number(a).toFixed(8);
	var x = Math.pow(zr, a);
	return Number(x).toFixed(9);
}
//容量修正系数
function Rlxzxs(a15, t) {
	var zr = 2.718282;
	var a = Number(-a15 * (t - 15) * (1 + 0.8 * a15 * (t - 15))).toFixed(8);
	var x = Math.pow(zr, a);
	return Number(x).toFixed(9);
}

module.exports = {
density
}